//
//  DateViewController.m
//  Park
//
//  Created by 123456 on 2017/12/27.
//  Copyright © 2017年 YTYangK. All rights reserved.
//

#import "DateViewController.h"
#import "GTLCalendarView.h"

@interface DateViewController ()<GTLCalendarViewDataSource, GTLCalendarViewDelegate>
{
    BOOL isclick;//判断是否有选择日期;
}
@end

@implementation DateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [self setupGTLCalendarViews];

    isclick = NO;

    UIButton *item = [[UIButton alloc]initWithFrame:CGRectMake(0,0, 60, 30)];
    [item setTitle:@"确定选择"forState:UIControlStateNormal];
    [item addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchDown];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:item];
    self.navigationItem.leftBarButtonItem=leftBarButtonItem;

    
}


#pragma mark - GTLCalendarViewDataSource

- (NSDate *)minimumDateForGTLCalendar {
    NSDate *date=[NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    NSString *dateStr;
    dateStr=[dateFormatter stringFromDate:date];
    return [dateFormatter dateFromString:dateStr];
}

- (NSDate *)maximumDateForGTLCalendar {
    return [NSDate new];
}

- (NSDate *)defaultSelectFromDate {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    return [dateFormatter dateFromString:self.fromDate];
}

- (NSDate *)defaultSelectToDate {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    return [dateFormatter dateFromString:self.toDate];
}

#pragma mark - GTLCalendarViewDelegate

- (NSInteger)rangeDaysForGTLCalendar {
    return 40;
}

- (void)selectNSStringFromDate:(NSString *)fromDate toDate:(NSString *)toDate {
    NSLog(@"fromDate: %@, toDate: %@", fromDate, toDate);
    
    self.fromDate = fromDate;
    self.toDate = toDate;
    NSLog(@"fromDate: %@, toDate: %@", self.fromDate, self.toDate);

    isclick = YES;
    
    
}

#pragma mark - private instance method

#pragma mark * init values

- (void)setupGTLCalendarViews {
    CGFloat screenWidth = CGRectGetWidth([UIScreen mainScreen].bounds);
    CGFloat screenHeight = CGRectGetHeight([UIScreen mainScreen].bounds);
    CGRect frame = CGRectMake(0, 0, screenWidth, screenHeight);
    
    GTLCalendarView *gtlCalendarView = [[GTLCalendarView alloc] initWithFrame:frame];
    gtlCalendarView.dataSource = self;
    gtlCalendarView.delegate = self;

    [self.view addSubview:gtlCalendarView];
}




-(void)click{
    
    if (isclick ==YES) {
        self.block(self.fromDate,self.toDate);

    }

    [self setAnimatedWithTransition];
    
    [self.navigationController popViewControllerAnimated:NO];
    
}

- (void)setAnimatedWithTransition{
    
    CATransition *animation = [CATransition animation];
    //动画时间
    animation.duration = 0.7f;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    //过渡效果
    animation.type = @"cube";
    //过渡方向
    animation.subtype = kCATransitionFromLeft;
    [self.navigationController.view.layer addAnimation:animation forKey:nil];
    
}


@end
